﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace AISFMIOperator
{
    class Company : Owner
    {
        public Company()
        { }

        public int CompanyID
        { get; set; }

        public string CompanyName
        { get; set; }

        public Address Address
        { get; set; }

        public string ContactPhone
        { get; set; }

        public string Email
        { get; set; }

        public string INN
        { get; set; }

        public string FullAddress
        {
            get
            {
                return Address.FullAddress;
            }
        }

        public string GetFullName()
        {
            return this.CompanyName + " ("+this.CompanyID+")";
        }

        public int GetOwnerID()
        {
            return this.CompanyID;
        }

        public byte GetOwnerType()
        {
            return 2;
        }

        public void Save()
        {
            SqlCommand command = new SqlCommand("CreateCompany");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@company_name", SqlDbType.VarChar, 128).Value = CompanyName;
            command.Parameters.Add("@address_id", SqlDbType.Int).Value = Address.AddressID;
            command.Parameters.Add("@contact_phone", SqlDbType.VarChar, 20).Value = ContactPhone;
            command.Parameters.Add("@INN", SqlDbType.VarChar, 12).Value = INN;
            command.Parameters.Add("@email", SqlDbType.VarChar, 100).Value = Email;
            command.Parameters.Add("@company_id", SqlDbType.Int).Direction = ParameterDirection.Output;
            SQLDBLayer.ExecSPNonQuery(command);
            this.CompanyID = (int)command.Parameters["@address_id"].Value;
        }

        public void Update()
        {
            SqlCommand command = new SqlCommand("UpdateCompany");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@company_name", SqlDbType.VarChar, 128).Value = CompanyName;
            //command.Parameters.Add("@address_id", SqlDbType.Int).Value = Address.AddressID;
            command.Parameters.Add("@contact_phone", SqlDbType.VarChar, 20).Value = ContactPhone;
            command.Parameters.Add("@INN", SqlDbType.VarChar, 12).Value = INN;
            command.Parameters.Add("@email", SqlDbType.VarChar, 100).Value = Email;
            command.Parameters.Add("@company_id", SqlDbType.Int).Direction = ParameterDirection.Output;
            SQLDBLayer.ExecSPNonQuery(command);
        }

        public void Delete()
        {
            SqlCommand command = new SqlCommand("DeleteCompany");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@company_id", SqlDbType.Int).Value = CompanyID;
            SQLDBLayer.ExecSPNonQuery(command);
        }

        public static List<Company> GetAllCompanies()
        {
            DataTable table = new DataTable();
            List<Company> objects = new List<Company>();
            SqlCommand command = new SqlCommand("GetCompanies");
            command.CommandType = CommandType.StoredProcedure;

            table = SQLDBLayer.ExecSPTable(command);

            foreach (DataRow row in table.Rows)
            {
                Company c = new Company();
                c.CompanyID = (int)row["company_id"];
                c.CompanyName = row["company_name"].ToString();
                c.ContactPhone = row["contact_phone"].ToString();
                c.Email = row["email"].ToString();
                c.INN = row["INN"].ToString();             

                int address_id = (int)row["address_id"];
                
                c.Address = (Address)Address.GetAllAddresses().Where(o => o.AddressID == address_id).ToList()[0];
                
                objects.Add(c);
            }

            return objects;
        }
    }
}
